<template>
    <div>
        <el-divider content-position="left">您具有一定的操作权限</el-divider>
    </div>

    <div>
        <el-divider>
            <span id="spanlittlepart">查询</span>
        </el-divider>
    </div>

    <!-- 查询 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <!-- <el-form-item label="植株名" style="width: 200px;">
            <el-select v-model="cropname" class="m-2" placeholder="请选择">
                <el-option v-for="item in cropnameList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item> -->
        <el-form-item label="植株名" style="width: 150px;">
            <el-input v-model="cropname" placeholder="cropname"></el-input>
        </el-form-item>
        <el-form-item label="贴士内容" style="width: 150px;">
            <el-input v-model="checkformInline.tiptext" placeholder="tiptext"></el-input>
        </el-form-item>
        <el-form-item style="width: 200px;">
            <el-button @click="search" type="primary" plain style="width: 200px;">查询</el-button>
        </el-form-item>
    </el-form>

    <el-divider>
        <el-icon><star-filled /></el-icon>
    </el-divider>

    <el-button type="primary" plain :onclick="toAddGrowTips" class="addbtn">
        <el-icon>
            <Edit />
        </el-icon>
        <span> 创建新的贴士</span>
    </el-button>

    <!-- Form -->
    <el-dialog v-model="dialogAddFormVisible" title="添加新项">
        <el-form :model="addform">
            <el-form-item label="植株名" :label-width="formLabelWidth">
                <el-select v-model="addform.cropsid" placeholder="请选择植株名">
                    <el-option v-for="item in tagsname" :key="item.cropsid" :label="item.cropname" :value="item.cropsid">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="贴士内容" :label-width="formLabelWidth">
                <el-input type="textarea" :rows="6" v-model="addform.tiptext" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="toSaveGrowTips">
                    提交
                </el-button>
            </span>
        </template>
    </el-dialog>

    <!-- 展示表格 -->
    <el-table :data="tableData" style="width: 100%" :highlight-current-row="true">
        <el-table-column prop="comings" label="植株名" width="100" />
        
        <el-table-column prop="tiptext" label="贴士内容" width="" />
        <el-table-column label="操作" width="200" style="margin-left: -30px;padding-left: -30px;overflow: visible;">
            <template #default="scope">
                <el-button link type="primary" size="small"
                    @click.prevent="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button link type="primary" size="small"
                    @click.prevent="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" :current-page="page" @update:current-page="handleCurrentChange"
        :total="total" :page-size="size" style="bottom: 0; text-align: center;margin-left: 35%;margin-top: 5px;" />

    <!-- Form -->
    <el-dialog v-model="dialogFormVisible" title="编辑">
        <el-form :model="form">
            <el-form-item label="贴士内容" :label-width="formLabelWidth">
                <el-input type="textarea" :rows="6" v-model="form.tiptext" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="onSaveInfo">
                    提交
                </el-button>
            </span>
        </template>
    </el-dialog>
    <!-- </div> -->
</template>
<script setup>
import {
    Guide,
    PictureFilled,
    User,
    HomeFilled,
    ChatLineSquare,
    Apple,
    Document,
    List,
    Tools,
    ShoppingCart,
    CollectionTag,
    Finished,
    InfoFilled,
    Cherry,
    Goods,
    Sell,
    ShoppingCartFull,
    Van,
    Sunrise,
    StarFilled,
    EditPen,
    Edit,
    Promotion,
    Grape,
    Pear,
} from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs';
import axios from 'axios';
const formLabelWidth = '140px';
</script>
<script>

export default {
    data() {
        return {
            baseURL: 'http://localhost:8080',
            formInline: {
                cropsid: 0,
                tipid: 0,
                tiptext: '',
            },
            checkformInline: {
                cropsid: 0,
                tipid: 0,
                tiptext: '',
            },
            cropnameList: [],
            cropname: '',
            dialogFormVisible: false,
            dialogAddFormVisible: false,
            showSearch: false,
            tableData: [],
            tableDataStore: [],
            tablecoverimgbigList: [],
            tablecoverimgbig: '',
            tablecoverimgsmallList: [],
            tablecoverimgsmall: '',
            tableinnerimgList: [],
            tableinnerimg: '',
            growtips: {
                cropsid: 0,
                tipid: 0,
                tiptext: '',
            },
            tagsname: [],
            form: {
                cropsid: 0,
                tipid: 0,
                tiptext: '',
            },
            addform: {
                cropsid: 0,
                tipid: 0,
                tiptext: '',
            },
            searchForm: [],
            searchtime: [],
            year: 0,
            month: 0,
            daynum: 0,
            page: 1, // 当前页码
            total: 0, // 总条数
            size: 10, // 每页的数据条数
            reallyTable: [],
            selData: [],
        }

    },
    methods: {
        init(){
            axios({
            method: 'post',
            url: 'http://localhost:8080/growtips/getgrowtipsbackend',
            withCredentials: true,
        }).then(res => {
            this.tableData = res.data.data;
            this.tableDataStore = res.data.data;
            console.log(res.data.data);
            this.tableData = res.data.data.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.total = res.data.data.length;
            this.selData = res.data.data;
        })
        },
        search() {
            let arr = []
            console.log(this.tableDataStore.length)
            console.log(this.cropname);
            for (let i = 0; i < this.tableDataStore.length; i++) {
                console.log(this.tableDataStore[i].cropsid);
                if (String(this.tableDataStore[i].tiptext).includes(this.checkformInline.tiptext)
                    && String(this.tableDataStore[i].comings).includes(this.cropname)
                ) {
                    arr.push(this.tableDataStore[i])
                }
            }
            console.log(arr);
            this.tableData = arr.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.selData = arr;
            this.total = arr.length;
        },
        // getName() {
        //     let arr = [];
        //     axios({
        //         method: 'post',
        //         url: 'http://localhost:8080/growtips/getname',
        //         withCredentials: true,
        //     }).then(res => {
        //         for(let i=0; i<res.data.data.length; i++){
        //             console.log(res.data.data);
        //             let obj = {
        //                 value: res.data.data[i].cropsid,
        //                 label: res.data.data[i].cropname
        //             }
        //             arr.push(obj)
        //         }
        //         console.log(arr);
        //         this.cropnameList = arr
        //     })
        // },
        //删除表格行
        handleDelete(index, row) {
            console.log(index, row);
            console.log(row.tipid);//可以获取到
            ElMessageBox.confirm(
                '确认删除本项吗？删除后无法找回:-0',
                'Warning',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    axios({
                        method: 'post',
                        url: 'http://localhost:8080/growtips/deletegrowtipbackend',
                        withCredentials: true,
                        data: {
                            tipid: row.tipid,
                        },
                    }).then(res => {
                        this.init()
                        ElMessage({
                            type: 'success',
                            message: '删除成功',
                        })
                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消删除操作',
                    })
                })
        },
        //编辑表格行
        handleEdit(index, row) {
            console.log(index, row);
            this.dialogFormVisible = true;
            this.form = row;
            // this.init()
        },
        onSaveInfo() {
            axios({
                method: 'post',
                url: 'http://localhost:8080/growtips/modifygrowtipsbackend',
                withCredentials: true,
                data: {
                    cropsid: this.form.cropsid,
                    tipid: this.form.tipid,
                    tiptext: this.form.tiptext,
                },
            }).then(res => {
                this.init()
            })

            this.dialogFormVisible = false;

        },
        formatDate(date) {
            var y = new Date(date).getFullYear();
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        Formatter(row, column) {
            if (row.create_time) {
                //console.log(new Date(row.create_time));
                this.day = new Date(row.date);
            }
            //console.log(this.formatDate(this.day));
            return this.formatDate(row.date);
        },
        backtosimpleformatDate(date) {
            var y = new Date(date).getFullYear();
            this.year = y;
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            this.month = m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            this.daynum = d;
            return y + '-' + m + '-' + d;
        },
        backtosimpleTime(date) {
            this.searchtime = this.checkformInline.date.split('-');
            this.backtosimpleformatDate(date);
            if (this.searchtime[0] == this.year && this.searchtime[1] == this.month && this.searchtime[2] == this.daynum) {
                return 'get';
            }
            else {
                return 'fail';
            }
        },
        toAddGrowTips() {
            this.dialogAddFormVisible = true;
        },
        toSaveGrowTips() {
            axios({
                method: 'post',
                url: 'http://localhost:8080/growtips/addgrowtipbackend',
                withCredentials: true,
                data: {
                    cropsid: this.addform.cropsid,
                    tiptext: this.addform.tiptext,
                },
            }).then(res => {
                this.init()
            })

            this.dialogAddFormVisible = false;
        },
        async handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page = val
            this.tableData = this.selData.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            // this.currentPage = val;
            const data = { 'page': val }
            // await this.onload(data)
        },
    },
    mounted() {
        //页面加载完成后，将数据库内的内容赋给定义的data变量
        this.init()
        axios({
                method: 'post',
                url: 'http://localhost:8080/tags/gettagsbackend',
                withCredentials: true,
            }).then(res => {
            this.tagsname = res.data.data
                console.log(res.data.data);
                
            })
        // this.getName()
    }
}
</script>
<style scoped>
#sidepart {
    width: 100%;
    position: absolute;
    top: 5px;
    font-family: '华文中宋';
}

#mainpart {
    width: 100%;
    position: absolute;
    top: 20px;
    /* left: 260px; */
    font-family: '华文中宋';
}

#spanpart {
    font-size: 30px;
    font-family: '华文中宋';
}

/* dialog */
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

#spanlittlepart {
    font-size: 15px;
    color: cadetblue;
    font-family: '华文中宋';
}

.bottom {
    width: 300px;
}

.addbtn {
    width: 80%;
    margin-bottom: 20px;
    font-family: '华文中宋';
    position: relative;
    left: -50px;
}

.spaninput {
    width: 950px;
    position: relative;
    top: 0px;
    left: 0px;
    margin-right: 10px;
    margin-top: 10px;
    margin-bottom: 20px;

}

.upload-demo {
    margin-bottom: 10px;
}

.contentPics {
    margin-top: 15px;
    margin-bottom: 15px;
}
</style>